package com.jtech.together.view.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.jtech.together.R;
import com.jtech.together.manager.UserManager;
import com.jtech.together.mvp.contract.SettingContract;
import com.jtech.together.mvp.presenter.SettingPresenter;
import com.jtech.together.util.Util;
import com.jtech.together.view.activity.base.BaseActivity;
import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 设置页面
 * Created by wuxubaiyang on 2017/5/12.
 */
public class SettingActivity extends BaseActivity implements SettingContract.View, View.OnClickListener {
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.textview_setting_version)
    TextView textViewVersion;

    private SettingContract.Presenter presenter;

    @Override
    protected void initVariables(Bundle bundle) {
        //绑定P类
        presenter = new SettingPresenter(getActivity(), this, bundle);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_setting);
        //设置标题栏
        setSupportActionBar(toolbar);
        //设置后退按钮点击事件
        toolbar.setNavigationOnClickListener(this);
    }

    @Override
    protected void loadData() {
        //设置默认参数
        setSettingInfo();
    }

    /**
     * 设置设置页面选项的默认参数
     */
    private void setSettingInfo() {
        //设置版本号
        textViewVersion.setText("v" + Util.getVersion(getActivity()));
    }

    @Override
    public void onClick(View v) {
        //后退
        onBackPressed();
    }

    /**
     * 设置菜单功能点击事件
     *
     * @param v
     */
    @OnClick({R.id.linearlayout_setting_grade, R.id.linearlayout_setting_share,
            R.id.linearlayout_setting_version})
    public void onOptionClick(View v) {
        switch (v.getId()) {
            case R.id.linearlayout_setting_grade://评分
                Util.appMark(getActivity());
                break;
            case R.id.linearlayout_setting_share://分享
                Util.shareText(getActivity(), getResources().getString(R.string.app_share_message));
                break;
            case R.id.linearlayout_setting_version://版本
                // TODO: 2017/5/13 当然啦，没有版本检查...
                break;
        }
    }

    /**
     * 注销点击事件
     */
    @OnClick(R.id.fab_setting)
    public void onLogoutClick() {
        new AlertDialog.Builder(getActivity())
                .setMessage("是否退出登录?")
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        UserManager.logout();
                        onBackPressed();
                    }
                }).show();
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TAG);
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TAG);
        MobclickAgent.onPause(this);
    }
}